package com.lv.fast.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lv.fast.DTO.SysUserDTO;
import com.lv.fast.DTO.SysUserPageQuery;
import com.lv.fast.VO.SysUserVO;
import com.lv.fast.annotation.SysLog;
import com.lv.fast.enums.OperateTypeEnum;
import com.lv.fast.enums.RestResultEnum;
import com.lv.fast.enums.UserStatusEnum;
import com.lv.fast.model.RestResult;
import com.lv.fast.service.SysUserService;
import com.lv.fast.util.HttpRequestUtil;
import com.lv.fast.util.JwtUtil;
import com.lv.fast.util.SysUserUtil;
import com.lv.fast.valid.group.AddStrategy;
import com.lv.fast.valid.group.UpdateStrategy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description
 * @Author jie.lv
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sys/user")
@Api(tags = "后台管理---用户模块")
@ApiSupport(author = "lv")
public class SysUserController {

    private final SysUserService sysUserService;

    @GetMapping("/list")
    @ApiOperation(value = "获取用户列表")
    @ApiOperationSupport(order = 1)
    @RequiresPermissions("sys:user:list")
    public RestResult list(@Validated SysUserPageQuery pageQuery){
        IPage<SysUserVO> page = sysUserService.list(pageQuery);
        return RestResult.success(page);
    }

    @GetMapping("/get/{id}")
    @ApiOperation(value = "查询用户详情")
    @ApiOperationSupport(order = 2)
    @RequiresPermissions("sys:user:info")
    public RestResult get(@PathVariable String id){
        SysUserDTO sysUserDTO = sysUserService.getSysUserById(id, true);
        SysUserVO sysUserVO = BeanUtil.copyProperties(sysUserDTO, SysUserVO.class);
        return RestResult.success(sysUserVO);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增用户")
    @ApiOperationSupport(order = 3)
    @SysLog(operateType = OperateTypeEnum.INSERT, value = "新增用户")
    @RequiresPermissions("sys:user:insert")
    public RestResult add(@RequestBody @Validated(AddStrategy.class) SysUserDTO userDTO){
        return RestResult.build(sysUserService.add(userDTO));
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改用户")
    @ApiOperationSupport(order = 4)
    @SysLog(operateType = OperateTypeEnum.INSERT, value = "修改用户")
    @RequiresPermissions("sys:user:update")
    public RestResult update(@RequestBody @Validated(UpdateStrategy.class) SysUserDTO userDTO){
        return RestResult.build(sysUserService.update(userDTO));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除用户")
    @ApiOperationSupport(order = 5)
    @SysLog(operateType = OperateTypeEnum.INSERT, value = "删除用户")
    @RequiresPermissions("sys:user:delete")
    public RestResult delete(@RequestBody String id){
        return RestResult.build(sysUserService.delete(id));
    }

    @PatchMapping("/lock")
    @ApiOperation(value = "锁定用户")
    @ApiOperationSupport(order = 6)
    @SysLog(operateType = OperateTypeEnum.INSERT, value = "锁定用户")
    @RequiresPermissions("sys:user:manage")
    public RestResult lock(@RequestBody String id){
        SysUserDTO userDTO = new SysUserDTO();
        userDTO.setId(id);
        userDTO.setStatus(UserStatusEnum.LOCK.getCode());
        return RestResult.build(sysUserService.update(userDTO));
    }

    @PatchMapping("/unlock")
    @ApiOperation(value = "解锁用户")
    @ApiOperationSupport(order = 7)
    @SysLog(operateType = OperateTypeEnum.INSERT, value = "解锁用户")
    @RequiresPermissions("sys:user:manage")
    public RestResult unlock(@RequestBody String id){
        SysUserDTO userDTO = new SysUserDTO();
        userDTO.setId(id);
        userDTO.setStatus(UserStatusEnum.VALID.getCode());
        return RestResult.build(sysUserService.update(userDTO));
    }

    @PatchMapping("/update-password")
    @ApiOperation(value = "修改密码")
    @ApiOperationSupport(order = 8)
    @SysLog(operateType = OperateTypeEnum.UPDATE, value = "修改用户密码")
    public RestResult updatePassword(@RequestBody String password){
        SysUserDTO userDTO = new SysUserDTO();
        userDTO.setId(SysUserUtil.getCurrentLoginUserId());
        userDTO.setStatus(password);
        return RestResult.build(sysUserService.update(userDTO));
    }

    @GetMapping("/get")
    @ApiOperation(value = "查询登录用户信息")
    @ApiOperationSupport(order = 9)
    public RestResult get(HttpServletRequest request){
        String token = HttpRequestUtil.getToken(request);
        if (token == null){
            return RestResult.build(RestResultEnum.TOKEN_IS_NULL);
        }
        String userId = JwtUtil.getTokenUserId(token);
        SysUserDTO sysUserDTO = sysUserService.getSysUserById(userId, true);
        SysUserVO sysUserVO = BeanUtil.copyProperties(sysUserDTO, SysUserVO.class);
        return RestResult.success(sysUserVO);
    }
}
